//--------------------------------------------------------------------------------
// This file is part of the Windows Workflow Foundation Sample Code
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation.  See these other
// materials for detailed information regarding Microsoft code samples.
// 
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//--------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Globalization;
using System.Collections;

namespace Microsoft.Samples.Workflow.UI.Design
{
    class TimeoutDurationConverter : TypeConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string) && value is TimeSpan)
            {
                TimeSpan timespan = (TimeSpan)value;
                return timespan.ToString();
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            object parsedTimespan = TimeSpan.Zero;
            string timeSpan = value as string;
            if (!String.IsNullOrEmpty(timeSpan))
            {
                //If this fails then an exception is thrown and the property set would fail
                try
                {
                    parsedTimespan = TimeSpan.Parse(timeSpan);
                    if (parsedTimespan != null && ((TimeSpan)parsedTimespan).Ticks < 0)
                    {
                        throw new Exception(string.Format(System.Globalization.CultureInfo.CurrentCulture, SR.GetString(SR.Error_InvalidTimespanFormat), timeSpan));
                    }
                }
                catch
                {
                    throw new Exception(SR.GetString(SR.Error_InvalidTimespanFormat, timeSpan));
                }
            }

            return parsedTimespan;
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            ArrayList standardValuesCollection = new ArrayList();
            standardValuesCollection.Add(new TimeSpan(0, 30, 0));
            standardValuesCollection.Add(new TimeSpan(1, 0, 0));
            standardValuesCollection.Add(new TimeSpan(12, 0, 0));
            standardValuesCollection.Add(new TimeSpan(1, 0, 0, 0));
            standardValuesCollection.Add(new TimeSpan(15, 0, 0, 0));
            standardValuesCollection.Add(new TimeSpan(30, 0, 0, 0));

            return new StandardValuesCollection(standardValuesCollection);
        }
    }
}
